import * as dayjs from 'dayjs'
import calendar from 'src/utils/calendar'

/**
 * 
 * @param date 纪念日日期
 * @param today 反算纪念日的标准
 * @param circulation 循环周期(0:每天1每隔一天2:每一星期3:每一个月,4:每年,99不循环)
 * @param lunarOrSolar 阴历还是阳历(0阴历1阳历)
 * @param countDown 累积或者倒数(0倒数1累积)
 * @returns 
 */
export function computeCommemorationDay(date:string,today:string,circulation:number,lunarOrSolar:number,countDown:number){
  //暂不考虑02-29的情况
  // const today=dayjs().format('YYYY-MM-DD');
  const intervalDay=dayjs(date).diff(today, 'day');
  let newCommemorationDate=dayjs().format('YYYY-MM-DD');
  let newIntervalDay=dayjs(newCommemorationDate).diff(today, 'day');
  const thisYear = dayjs().get('year');
  const thisMonth = dayjs().get('month');
  if(intervalDay<0 && countDown===0){
    //已过期
    if(circulation!==99){
      //需要循环
      switch (circulation) {
        case 1:
          //每隔一天循环
          if(Math.abs(intervalDay)%2!==0){
            newCommemorationDate=dayjs().add(1,'day').format('YYYY-MM-DD');
          }
          newIntervalDay=dayjs(newCommemorationDate).diff(today, 'day');
          break;
        case 2:
          //每一星期循环
          const week = dayjs(date).day();
          newCommemorationDate = dayjs(newCommemorationDate).day(week).format('YYYY-MM-DD');
          newIntervalDay = dayjs(newCommemorationDate).diff(today, 'day');
          while(newIntervalDay<0){
            newCommemorationDate = dayjs(newCommemorationDate).day(week+7).format('YYYY-MM-DD');
            newIntervalDay = dayjs(newCommemorationDate).diff(today, 'day');
          }
          break;
        case 3:
          //每一个月循环
          newCommemorationDate = dayjs(date)
            .year(thisYear)
            .month(thisMonth+1)
            .format('YYYY-MM-DD');
          newIntervalDay = dayjs(newCommemorationDate).diff(today, 'day');
          break;
        case 4:
          //每年
          newCommemorationDate = dayjs(date).year(thisYear).format('YYYY-MM-DD');
          newIntervalDay = dayjs(newCommemorationDate).diff(today, 'day');
          if(newIntervalDay<0){
            newCommemorationDate = dayjs(newCommemorationDate).year(thisYear+1).format('YYYY-MM-DD');
            newIntervalDay = dayjs(newCommemorationDate).diff(today, 'day');
          }
          break;
        default:
          break;
      }
    }
  }
  else if(countDown===1){
    newCommemorationDate = dayjs(date).format('YYYY-MM-DD');
    newIntervalDay = dayjs(newCommemorationDate).diff(today, 'day');
    // while(newIntervalDay<0){
    //   newCommemorationDate = dayjs(newCommemorationDate).year(thisYear+1).format('YYYY-MM-DD');
    //   newIntervalDay = dayjs(newCommemorationDate).diff(today, 'day');
    // }
    // newIntervalDay = dayjs(date).diff(today, 'day');
  }
  else{
    //未过期
    newCommemorationDate=dayjs(date).format('YYYY-MM-DD');
    newIntervalDay=dayjs(newCommemorationDate).diff(today, 'day');
  }
  const lunarCalendar=calendar.lunar2solar(
    dayjs(newCommemorationDate).get('year'),
    dayjs(newCommemorationDate).get('month'),
    dayjs(newCommemorationDate).get('day')
  );
  return {
    CommemorationDate:newCommemorationDate,
    LunarDate:lunarOrSolar===0?lunarCalendar.IMonthCn+lunarCalendar.IDayCn:'',
    LunarNumDate:lunarOrSolar===0?dayjs(lunarCalendar.lunarDate).format('MM-DD'):'',
    SolarDate:dayjs(newCommemorationDate).format('MM-DD'),
    Weekly:computeWeekly(dayjs(newCommemorationDate).day()),
    IntervalDay:newIntervalDay
  };
}

/**
 * 计算纪念日是周几
 * @param week 
 * @returns 
 */
export function computeWeekly(week:number){
  let weekly='';
  switch (week) {
    case 0:
      weekly = "周日";
      break;
    case 1:
      weekly = "周一";
      break;
    case 2:
      weekly = "周二";
      break;
    case 3:
      weekly = "周三";
      break;
    case 4:
      weekly = "周四";
      break;
    case 5:
      weekly = "周五";
      break;
    case 6:
      weekly = "周六";
      break;
    default:
      break;
  }
  return weekly
}

/**
 * 计算发送短信的日期
 * @param date 日期
 * @param countDown  累积或者倒数(0倒数1累积)
 * @param remindDate 提醒时间(0:当天1:提前一天2:提前两天3:提前三天4:提前一星期5:提前一个月6:每天提醒,99不提醒)
 * @returns 
 */
export function computeSendSmsDate(date:string,countDown:number,remindDate:number){
  let newDate=date;
  if(countDown===1){
    const today=dayjs().format('YYYY-MM-DD');
    const intervalDay=dayjs(newDate).diff(today, 'day');
    const thisYear = dayjs().get('year');
    if(intervalDay<0){
      let newCommemorationDate = dayjs(date).year(thisYear).format('YYYY-MM-DD');
      let newIntervalDay = dayjs(newCommemorationDate).diff(today, 'day');
      if(newIntervalDay<0){
        newCommemorationDate = dayjs(newCommemorationDate).year(thisYear+1).format('YYYY-MM-DD');
        newIntervalDay = dayjs(newCommemorationDate).diff(today, 'day');
      }
      newDate=newCommemorationDate
    }
  }
  const sendSmsIntervalDate = dayjs(newDate);
  let sendSmsDate='';
  switch (remindDate) {
    case 0:
      // 当天
      sendSmsDate = sendSmsIntervalDate.format('YYYY-MM-DD');
      break;
    case 1:
      // 提前一天
      sendSmsDate = sendSmsIntervalDate
        .add(-1, 'day')
        .format('YYYY-MM-DD');
      break;
    case 2:
      // 提前两天
      sendSmsDate = sendSmsIntervalDate
        .add(-2, 'day')
        .format('YYYY-MM-DD');
      break;
    case 3:
      // 提前三天
      sendSmsDate = sendSmsIntervalDate
        .add(-3, 'day')
        .format('YYYY-MM-DD');
      break;
    case 4:
      // 提前一周
      sendSmsDate = sendSmsIntervalDate
        .add(-7, 'day')
        .format('YYYY-MM-DD');
      break;
    case 5:
      // 提前一月
      sendSmsDate = sendSmsIntervalDate
        .add(-1, 'month')
        .format('YYYY-MM-DD');
      break;
    case 99:
      // 不提醒
      sendSmsDate = '';
      break;
    default:
      sendSmsDate = '';
      break;
  }
  return sendSmsDate;
}